package com.livemeter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Log;

/*
 * Represents data behind all polls of current master.
 * */
public class Poll 
{
	private String title;
	private String question;
	private boolean permanent;
	private ArrayList<PollOption> options = new ArrayList<PollOption>();
	
	private transient boolean m_isActive = false; 
	private transient boolean m_hasBeenShown = false; 

	public boolean isActive() {return m_isActive;}
	public void setActive(boolean active) {m_isActive = active;}
	public boolean hasBeenShown() {return m_hasBeenShown;}
	public void setHasBeenShown() {m_hasBeenShown = true;}
	
	private static ArrayList<Poll> allPolls = new ArrayList<Poll>();
	private static boolean changed; //whether new data has to be stored to filesystem
	
	public static void clearAll()
	{
		allPolls.clear();
		Poll.changed = true;
	}
	
	private Poll() //only allow usage via static methods
	{}
	
	public String getTitle()
	{
		return this.title;
	}
	
	public String getQuestion()
	{
		return this.question;
	}
	
	public ArrayList<PollOption> getOptions(boolean toChangeOptions)
	{
		if(toChangeOptions)
			Poll.changed = true;
		
		return this.options;
	}
		
	public boolean isPermanent()
	{
		return this.permanent;
	}
	
	public static Poll addNewPoll(String title, String question, boolean permanent)
	{
		Poll newPoll = new Poll();
		newPoll.title = title;
		newPoll.question = question;
		newPoll.permanent = permanent;
		
		Poll.allPolls.add(newPoll);
		
		if(permanent)
			Poll.changed = true;
		
		return newPoll;
	}
	
	public static Poll getPoll(String pollTitle)
	{
		for(Poll currentPoll : Poll.allPolls)
		{
			if(currentPoll.title.equalsIgnoreCase(pollTitle))
				return currentPoll;
		}
		
		return null;
	}
	
	/*
	 * Returns the titles of all available polls.
	 * */
	public static String[] getAllPollTitles()
	{
		int count = Poll.allPolls.size();
		
		if(count == 0)
			return null;
		
		String[] pollTitles = new String[count];
		
		for(int i = 0; i < count; i++)
			pollTitles[i] = Poll.allPolls.get(i).title;
		
		return pollTitles;
	}
	
	public static ArrayList<Poll> getActivePolls()
	{
		ArrayList<Poll> result = new ArrayList<Poll>();
		for (Poll p : Poll.allPolls)
		{
			if (p.isActive())
				result.add(p);
		}
		return result;
	}

	public static String[] getActivatedPollTitles()
	{
		ArrayList<String> polls = new ArrayList<String>();
		
		for(Poll currentPoll : Poll.allPolls)
		{
			if(currentPoll.isActive())
				polls.add(currentPoll.title);
		}		
		
		if(polls.size() > 0)
			return (String[])polls.toArray(new String[polls.size()]);
		
		return new String[0];
	}
	
	/*
    * Checks whether poll parameters are not empty and poll title is unique.
    * */
 	public static boolean checkPoll(String title, String question)
	{
		if(title.length() == 0 || question.length() == 0)
			return false;
		
		//check whether title is unique
		for(Poll currentPoll : Poll.allPolls)
		{
			if(currentPoll.title.compareTo(title) == 0)
				return false;
		}
		
		return true;
	}

	/*
	 * Checks whether poll option is unique.
	 * */
	public static boolean checkPollOption(Poll poll, String option)
	{
		if(option.length() == 0)
			return false;
		
		if(poll == null)
			return false;
		
		//check whether option is unique within poll
		for(PollOption currentOption : poll.options)
		{
			if(currentOption.getOption().equalsIgnoreCase(option))
				return false;
		}
		
		return true;
	}
	
	public static void addPollOption(Poll poll, PollOption option)
	{
		if(poll == null)
			return;
		
		poll.options.add(option);
		
		if(poll.permanent)
			Poll.changed = true;
	}
	
	public static void removePoll(String pollTitle)
	{
		final int count = Poll.allPolls.size();
		
		for(int i = 0; i < count; i++)
		{
			if(Poll.allPolls.get(i).title.compareToIgnoreCase(pollTitle) == 0)
			{
				Poll.allPolls.remove(i);
				Poll.changed = true;
				break;
			}
		}
	}
	
	
	public static void loadPolls(Activity context)
	{
		Poll.allPolls.clear();
		
		SharedPreferences settings = context.getSharedPreferences("Polls", 0);
	    int countPolls = settings.getInt("pollCount", 0);
	       
	    for(int i = 0; i < countPolls; i++)
	    {
	    	String pollEncoded = settings.getString("poll" + i, "");
	    	
	    	if(pollEncoded.length() == 0)
	    		continue;
	    	
	    	String[] pollProps = pollEncoded.split("\\|");
	    	
	    	Poll currentPoll = Poll.addNewPoll(pollProps[0], pollProps[1], true);
	    	
	    	//load poll options
	    	String pollOptionsEncoded = settings.getString("polloptions" + i, "");
	    	
	    	if(pollOptionsEncoded.length() == 0)
	    		continue;
	    	
	    	String[] loadedPollOptions = pollOptionsEncoded.split("\\|");
	    	
	    	for(int j = 0; j < loadedPollOptions.length; j++)
	    	{
	    		currentPoll.options.add(new PollOption(loadedPollOptions[j]));
	    	}
	    } 
	    
	    Poll.changed = false;
	}
	
	public static void savePolls(Activity context)
	{
		if(!Poll.changed)
			return;

		SharedPreferences settings = context.getSharedPreferences("Polls", 0);
		SharedPreferences.Editor editor = settings.edit();
		
		final int count = Poll.allPolls.size();
		
		int minusElements = 0;
		int continousIndex = 0;
		
		for(int i = 0; i < count; i++)
		{
			Poll currentPoll = Poll.allPolls.get(i);
			
			if(!currentPoll.permanent)
			{
				minusElements++;
				continue;
			}
			else
			{
				editor.putString("poll" + continousIndex, currentPoll.title + "|" + currentPoll.question);
			}
			
			
			ArrayList<PollOption> pollOptions = currentPoll.getOptions(false);
			final int countOptions = pollOptions.size();
			
			String pollOptionsEncoded = "";
			
			//put poll options in another string to save it
			for(int j = 0; j < countOptions; j++)
			{
				pollOptionsEncoded = pollOptionsEncoded.concat(pollOptions.get(j).getOption());
				
				if(j < countOptions - 1)
					pollOptionsEncoded = pollOptionsEncoded.concat("|");
			}
			
			if(countOptions > 0)
			{
				editor.putString("polloptions" + continousIndex, pollOptionsEncoded);
			}
			
			continousIndex++;
		}
		
		editor.putInt("pollCount", count - minusElements);
		
		// Commit the edits!
		editor.commit();
		
		Poll.changed = false;
	}
}


